// pages/searchList/searchList.js
const app = getApp()
var url = require('../../utils/url.js');
var page = 1;
var last_page = 0;
var type;
var keyword;
var priceType=1;
var priceTap=1;
var navBar=0;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    if (navBar==0){
      console.log(page)
      wx.request({
        url: url.URl + 'search',
        data: { page: page, keywords: keyword,type:type },
        method: 'post',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res)
          wx.hideLoading();
          var shops = res.data.data.data;
          if (type == 2) {
            var bill = "bill"
            for (var i = 0; i < shops.length; i++) {
              console.log(i)
              var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
              shops[i].bill = billF.toFixed(2) + "%"
            }
          }
          that.setData({
            shops: that.data.shops.concat(shops)
          });
          page++;
        }
      });
    } else if (navBar == 1){
      wx.request({
        url: url.URl + 'search',
        data: { page: page, keywords: keyword, type: type, new: 1 },
        method: 'post',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          var shops = res.data.data.data;
          if (type == 2) {
            var bill = "bill"
            for (var i = 0; i < shops.length; i++) {
              console.log(i)
              var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
              shops[i].bill = billF.toFixed(2) + "%"
            }
          }
          that.setData({
            shops: that.data.shops.concat(shops)
          });
          page++;
        }
      });
    } else if (navBar == 2) {
      wx.request({
        url: url.URl + 'search',
        data: { page: page, keywords: keyword, type: type, goods_salenum: 1 },
        method: 'post',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          var shops = res.data.data.data;
          if (type == 2) {
            var bill = "bill"
            for (var i = 0; i < shops.length; i++) {
              console.log(i)
              var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
              shops[i].bill = billF.toFixed(2) + "%"
            }
          }
          that.setData({
            shops: that.data.shops.concat(shops)
          });
          page++;
        }
      });
    } else if (navBar == 3) {
      console.log(priceType)
      wx.request({
        url: url.URl + 'search',
        data: { page: page, keywords: keyword, type: type, price: priceType },
        method: 'post',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          var shops = res.data.data.data;
          if (type == 2) {
            var bill = "bill"
            for (var i = 0; i < shops.length; i++) {
              console.log(i)
              var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
              shops[i].bill = billF.toFixed(2) + "%"
            }
          }
          that.setData({
            shops: that.data.shops.concat(shops)
          });
          page++;
        }
      });
    }
    
  } else {
    wx.hideLoading();
    that.setData({
      nodata: 1,
      skill: true
    })
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navTab: ['默认', '最新', '热销', '价格'],
    currentNavtab: 0,
    shops: [],
    listStyle: 1,
    priceType:2,
    type:type,
    queS:true
  },
  listStyle: function (e) {
    console.log(e)
    var that = this;
    let idx = e.currentTarget.dataset.index;
    if (idx == 1) {
      that.setData({
        listStyle: 2
      })
    } else if (idx == 2) {
      that.setData({
        listStyle: 1
      })
    }
  },
  switchTab: function (e) {
    var that = this;
    let idx = e.currentTarget.dataset.idx;
    let currentNavtab = that.data.currentNavtab;
    if (idx == currentNavtab&&idx!=3) {
      return false;
    } else {
      page = 1
      that.setData({
        shops: []
      })
      if (idx==0){
        navBar=0;
        that.getGoods();
        priceType = 1;
        priceTap = 1;
        that.setData({
          priceType: 2
        })
      }else if(idx==1){
        navBar = 1;
        that.getNewGoods()
        priceType = 1;
        priceTap = 1;
        that.setData({
          priceType: 2
        })
      }else if(idx==2){
        navBar = 2;
        that.getsalenumGoods()
        priceType = 1;
        priceTap = 1;
        that.setData({
          priceType: 2
        })
      } else if (idx == 3) {
        navBar = 3;
        console.log(priceType)
        console.log(priceTap)
        if (priceTap==1){
          priceType = 1;
          that.getPriceGoods()
          that.setData({
            priceType:1
          })
          priceTap = 2;
        } else if (priceTap == 2){
          priceType = 0;
          that.getPriceGoods()
          that.setData({
            priceType: 0
          })
          priceTap = 1;
        }
      }
      that.setData({
        currentNavtab: idx
      })
    }
  },
  getsalenumGoods() {
    var that = this;
    if (page > 1) {
      page--;
    }
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'search',
      data: { page: page, keywords: keyword, type: type, goods_salenum:1},
      method: 'post',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg;
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          var shops = res.data.data.data;
          if (type == 2) {
            var bill = "bill"
            for (var i = 0; i < shops.length; i++) {
              console.log(i)
              var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
              shops[i].bill = billF.toFixed(2) + "%"
            }
          }
          that.setData({
            shops: shops
          })
          that.setData({
            queS: true
          })
        } else if (res.data.code == 201) {
          that.setData({
            queS: false
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getGoods() {
    var that = this;
    that.setData({
      shops:[]
    })
    if (page > 1) {
      page--;
    }
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'search',
      data: { page: page, keywords: keyword, type: type },
      method: 'post',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg;
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          var shops = res.data.data.data;
          if (type == 2) {
            var bill = "bill"
            for (var i = 0; i < shops.length; i++) {
              console.log(i)
              var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
              shops[i].bill = billF.toFixed(2) + "%"
            }
          }
        that.setData({
          shops: shops
        })
        }else if(res.data.code==201){
          that.setData({
            queS:false
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getNewGoods() {
    var that = this;
    if (page > 1) {
      page--;
    }
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'search',
      data: { page: page, keywords: keyword, type: type, new:1 },
      method: 'post',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg;
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          var shops = res.data.data.data;
          if (type == 2) {
            var bill = "bill"
            for (var i = 0; i < shops.length; i++) {
              console.log(i)
              var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
              shops[i].bill = billF.toFixed(2) + "%"
            }
          }
          that.setData({
            shops: shops
          })
          that.setData({
            queS: true
          })
        } else if (res.data.code == 201) {
          that.setData({
            queS: false
          })
        }  else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getPriceGoods() {
    var that = this;
  
    if (page > 1) {
      page--;
    }
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'search',
      data: { page: page, keywords: keyword, type: type, price: priceType },
      method: 'post',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg;
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          var shops = res.data.data.data;
          if(type==2){
            var bill = "bill"
            for (var i = 0; i < shops.length; i++) {
              console.log(i)
              var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
              shops[i].bill = billF.toFixed(2) + "%"
            }
          }
          that.setData({
            shops: shops
          })
          that.setData({
            queS: true
          })
        } else if (res.data.code == 201) {
          that.setData({
            queS: false
          })
        }  else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  onChange(e) {
    this.setData({
      value: e.detail
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options)
      type = options.type;
      this.setData({
        type: type
      })
  },
  onSearch(event) {
    page=1;
    this.setData({
      shop:[]
    })
    keyword = this.data.value

    
    if (this.data.value) {
      console.log(keyword)
      this.getGoods()
    }else{

    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    priceType = 1;
    priceTap = 1;
    navBar=0;
    page=1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(page)
    var that = this;
    if (that.data.index != 0) {
      loadMore(that)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})